package com.mf.controller.basic;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.mf.aspect.debounces.Debounce;
import com.mf.aspect.logApi.LogApi;
import com.mf.entity.req.basic.reqPublic.ReqPublicPaging;
import com.mf.groups.GroupsAddInterface;
import com.mf.groups.GroupsAdminUpdateInterface;
import com.mf.groups.GroupsUpdateInterface;
import com.mf.groups.GroupsUserUpdateInterface;
import com.mf.result.Result;
import com.mf.service.basic.ServiceUserRole;
import com.mf.utils.UtilConverter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.entity.dto.basic.DtoUser;
import com.mf.entity.req.basic.ReqUser;
import com.mf.service.basic.ServiceUser;
import com.mf.entity.req.basic.search.SearchReqUser;

import java.util.List;

/**
 * 用户表
 */
@RestController
@RequestMapping("user")
@SaCheckLogin
@Tag(name = "用户表")
@Debounce
public class ControllerUser {
    @Resource
    private BCryptPasswordEncoder passwordEncoder;

    @Resource
    private ServiceUser serviceuser;

    @Resource
    private ServiceUserRole serviceuserRole;

    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("user:search")
    public Result<Page<DtoUser>> findPage(@RequestBody SearchReqUser searchReqUser) {
        Page<DtoUser> pageByParams = serviceuser.findPageByParams(searchReqUser);
        pageByParams.getRecords().forEach(item -> {
            item.setPassword(null);
        });
        return Result.success(pageByParams);
    }

    @Operation(summary = "列表查询")
    @SaCheckPermission("user:search")
    @PostMapping("/list")
    public Result<List<DtoUser>> list(@RequestBody SearchReqUser searchReqUser) {
        List<DtoUser> listByParams = serviceuser.findListByParams(searchReqUser);
        listByParams.forEach(item -> {
            item.setPassword(null);
        });
        return Result.success(listByParams);
    }

    @Operation(summary = "根据角色id查询已关联用户")
    @SaCheckPermission({"user:search","role:upUser"})
    @PostMapping("/pageUserByRole/{roleId}")
    public Result<Page<DtoUser>> pageUserByRole(@RequestBody() ReqPublicPaging reqPublicPaging,
                                                @PathVariable("roleId") Integer roleId) {

        return Result.success(serviceuserRole.pageUserByRole(roleId,reqPublicPaging));
    }

    @Operation(summary = "根据角色id查询未关联用户")
    @SaCheckPermission({"user:search","role:upUser"})
    @PostMapping("/pageUserByNoRole/{roleId}")
    public Result<Page<DtoUser>> pageUserByNoRole(@RequestBody() ReqPublicPaging reqPublicPaging,
                                                  @PathVariable("roleId") Integer roleId) {

        return Result.success(serviceuserRole.pageUserByNoRole(roleId,reqPublicPaging));
    }

    @Operation(summary = "保存")
    @LogApi("保存用户")
    @SaCheckPermission("user:edit")
    @PostMapping("/save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqUser reqUser) {
        // 对密码进行加密
        reqUser.setPassword(passwordEncoder.encode(reqUser.getPassword()));
        DtoUser dtoUser = UtilConverter.converter(reqUser, DtoUser.class);
        dtoUser.setUpUserId(StpUtil.getSession().getModel(SaSession.USER, DtoUser.class).getId());
        serviceuser.save(dtoUser);
        return Result.success();
    }

    @Operation(summary = "修改")
    @SaCheckPermission("user:edit")
    @LogApi("修改用户")
    @PostMapping("/update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqUser reqUser) {
        DtoUser dtoUser = UtilConverter.converter(reqUser, DtoUser.class);
        dtoUser.setUpUserId(StpUtil.getSession().getModel(SaSession.USER, DtoUser.class).getId());
        serviceuser.updateById(dtoUser);
        return Result.success();
    }

    @Operation(summary = "管理员修改密码")
    @SaCheckPermission("user:adminPwd")
    @LogApi("管理员修改密码")
    @PostMapping("/adminUpdatePwd")
    public Result<Void> adminUpdatePwd(@Validated(GroupsAdminUpdateInterface.class) @RequestBody ReqUser reqUser) {
        serviceuser.adminUpdatePwd(reqUser);
        return Result.success();
    }

    @Operation(summary = "用户修改密码")
    @LogApi("用户修改密码")
    @PostMapping("/updatePwd")
    public Result<Void> updatePwd(@Validated(GroupsUserUpdateInterface.class) @RequestBody ReqUser reqUser) {
        serviceuser.updatePwd(reqUser);
        return Result.success();
    }
}
